<?php
set_time_limit(0);
declare(ticks=1);

class PhpDocParserRun
{
    const UPDATE_RESULTS_FILE = 'up.txt';
    
    protected $projectPath;
    protected $resPath;
    protected $pidFile;
    public $daemonName;
    
    public function __construct($projectPath, $resPath)
    {
        $this->daemonName = 'phpdoc_parser_runer';
        $this->pidFile = '/tmp/'.strtolower( $this->daemonName ).".pid";                
        
        $this->projectPath = $projectPath;
        $this->resPath = $resPath;
    }
    
    function __destruct() 
    {
        @unlink( $this->pidFile );
    }    

    public function isActive()
    {
        if( is_file( $this->pidFile ) ) {
            $pid = file_get_contents( $this->pidFile );            
            if(posix_kill($pid,0)) {
                return true;
            } elseif( !unlink( $this->pidFile ) ) {
                exit(-1);
            }
        }
        return false;
    }    
    
    public function run()
    {
        @unlink($this->resPath . self::UPDATE_RESULTS_FILE);
        file_put_contents( $this->pidFile, getmypid() );
        
        print 'Start updating'."\n";
        $this->svnUpdate();
        print 'Updated'."\n";
        if (!file_exists($this->resPath . self::UPDATE_RESULTS_FILE)) {
            exit('File ' .self::UPDATE_RESULTS_FILE. ' with updating results not found in ' .$this->projectPath);
        }
        $content = file_get_contents($this->resPath . self::UPDATE_RESULTS_FILE);
        $isSourcesChanged = (strpos($content,'Updated to revision') !== false);
        if($isSourcesChanged) {
            $this->runPhpDoc();
            $this->runParser();
        } else {
            exit('Project sources has not been changed'."\n");
        }
        unlink( $this->pidFile );
    }
    
    public function svnUpdate()
    {
        exec( '/usr/local/bin/svn up '.$this->projectPath.' > ' . $this->resPath . self::UPDATE_RESULTS_FILE );
    }
    
    public function runPhpDoc()
    {
        print 'Remove previouse results'."\n";        
        $this->deleteDirectory($this->resPath . 'docs');
        print 'Start PhpDoc'."\n";
        exec('cd ' . $this->projectPath);
        exec('/usr/local/bin/php /bazalt/utils/PhpDocumentor/phpdoc -c /bazalt/configs/makedocs.ini 2>&1 ', $out );
        print 'Out' . implode("\n",$out) . "\n\n";
    }
    
    public function deleteDirectory($dir) 
    {
        if (!file_exists($dir)) 
            return true;
        if (!is_dir($dir))
            return unlink($dir);
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..')
                continue;
            if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item))
                return false;
        }
        return rmdir($dir);
    }
    
    public function runParser()
    {
        print 'Start parser'."\n";
        $this->deleteDirectory($this->resPath . 'source');
        exec( '/usr/local/bin/mono /bazalt/utils/cs_builder/bin/builder.exe 2>&1 ', $out );
        print 'Out' . implode("\n",$out) . "\n\n";
    }
}


$projectPath = '/bazalt/projects/framework/';
$resPath = '/bazalt/builds/bazalt/';
$o = new PhpDocParserRun( $projectPath, $resPath );
if( $o->isActive() ) {
    exit($o->daemonName . ' is already runed'."\n");
}
$o->run();